from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
from baixinservice.models import *

import sys
reload(sys)
sys.setdefaultencoding('utf8')

def getInformationByCategoryId(request,categoryId=1):
	try:
		_ics = InformationCategory.objects.all()

		_c = InformationCategory.objects.get(id = categoryId)

		_is = Information.objects.order_by('-createTime').filter(category = _c)[0:4]
	except Exception,e:
		return HttpResponse(e.message)
	else:
		return render_to_response('appSide/healthNews.html',{ 'ics':_ics, 'is':_is, 'categoryId':categoryId })

def getInformationDetail(request,informationId):
	try:
		_c = Information.objects.get(id = informationId)

		_c.createTime = _c.createTime.strftime('%Y-%m-%d %I:%M:%S')
		_c.modifyTime = _c.modifyTime.strftime('%Y-%m-%d %I:%M:%S')

	except Exception,e:
		return HttpResponse(e.message or e.__str__())
	else:
		return render_to_response('appSide/healthNewsDetails.html',{ 'c':_c })

def getCategoryInfoByIndex(request,categoryId,startIndex):
	try:
		_startIndex = int(startIndex)

		_c = InformationCategory.objects.get(id = categoryId)

		_is = Information.objects.order_by('-createTime').filter(category = _c)

		_endIndex = _startIndex + 4

		if _endIndex >= len(_is):
			_endIndex = len(_is)

		_is = _is[ _startIndex : _endIndex ]

		_tmpData = []

		for _i in _is:
			objStr = simplejson.dumps(_i.getObj())
			_tmpData.append(objStr)

	except Exception,e:
		result = { 'error':e.message or e.__str__() }
		return HttpResponse(simplejson.dumps(result))
	else:
		result = { 'data':_tmpData, 'endIndex':_endIndex }
		return HttpResponse(simplejson.dumps(result))